package servlet;
import java.io.*;
import java.util.List;

import javabean.Factory;
import javabean.book.Book;

import javax.servlet.*;
import javax.servlet.http.*;

import net.sf.json.JSONArray;


public class QueryBookByAjax extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8538076142868886263L;
	
	public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		String keyword = request.getParameter("keyword");
		String type = request.getParameter("type");
		String index1 = request.getParameter("index");
		String count1 = request.getParameter("count");
		int index=1,count=20;
		List<Book> all = null;
		if(index1 != null){
			index = Integer.parseInt(index1);
		}
		if(count1 != null){
			count = Integer.parseInt(count1);
		}
		try{
			if(type!=null && !"".equals(type)){
				all = Factory.getBookDAOInstance().queryAllBookByType(type, index, count);
			}else{
				if(keyword!=null && !"".equals(keyword)){
					all = Factory.getBookDAOInstance().queryAllBookByLike(keyword,index,count) ;
				}else{
					all = Factory.getBookDAOInstance().queryAllBookByLike("",index,count) ;
				}
			}
			response.setContentType("text/html;charset=UTF-8"); 
			response.getWriter().write("{\"bookData\":"+JSONArray.fromObject(all).toString()+",\"success\":true}");
			
		}catch(Exception ex){
			response.getWriter().write("{\"success\":false,\"msg\":\"服务器出错了\"}");
		}
	}
}
